import { defineStore } from 'pinia';
import pinia from '@/store';
import { userLogin, refreshUserInfo } from '@/api/user';
import { IUserLoginState, LoginRequest } from './types';
export const useUserStoreHook = defineStore('userinfo', {
    state: (): IUserLoginState => {
        return {
            username: 'admin',
            accessToken: '',
            roles: []
        };
    },
    getters: {},
    actions: {
        storeUserLogin(data: LoginRequest) {
            return userLogin(data).then((res) => {
                console.log(this);
                this.accessToken = res.accessToken;
                this.username = res.username;
                this.roles = res.roles;
            });
        },
        storeRefreshUserInfo() {
            return refreshUserInfo()
                .then((res) => {
                    this.accessToken = res.accessToken;
                    this.username = res.username;
                    this.roles = res.roles;
                })
                .catch(() => {
                    this.accessToken = '';
                });
        }
    },
    persist: {
        key: 'userInfo',
        storage: sessionStorage,
        paths: ['accessToken']
    }
});
export function useUserStore() {
    return useUserStoreHook(pinia);
}
